<template>
<!-- 微信群机器人 -->
  <!-- 搜索工作栏 -->
   <div v-show="setView==='list'">
    <ContentWrap>
      <Search :schema="allSchemas.searchSchema" @search="setSearchParams" :show-reset="false" :show-search="false"  @reset="setSearchParams">
        <!-- 新增等操作按钮 -->
        <template #actionMore>
          <el-button
            type="primary"
            @click="openForm('create')"
          >
            新增
          </el-button>
        </template>
      </Search>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <Table
        :columns="allSchemas.tableColumns"
        :data="tableObject.tableList"
        :loading="loading"
        :pagination="{
          total: tableObject.total
        }"
        v-model:pageSize="tableObject.pageSize"
        v-model:currentPage="tableObject.currentPage"
      >
      <template #address="{ row }">
        <el-button
            link
            type="primary"
          @click="openDetail(row.id)"
          >
          {{ row.address }}
          </el-button>
        </template>
      <template #isTest="{ row }">
          <span>{{ row.isTest !=1?'是':'否' }}</span>
        </template>
      <template #isValid="{ row }">
          <el-switch
            v-model="row.isValid"
            active-value="0"
            inactive-value="1"
            inline-prompt
            :active-icon="Check" :inactive-icon="Close"
            @change="handleStatusChange(row)"
          />
        </template>
        <template #action="{ row }">
          <el-button
            link
            type="primary"
            @click="openForm('update', row.id)"
            :disabled="row.isValid=='0'"
          >
            编辑
          </el-button>
          <!-- <el-button
            link
            type="primary"
            @click="openDetail(row.id)"
          >
            详情
          </el-button> -->
          <el-button
            link
            type="danger"
            @click="handleDelete(row.id)"
            :disabled="row.isValid=='0'"
          >
            删除
          </el-button>
        </template>
      </Table>
    </ContentWrap>
  </div>

  <!-- 表单弹窗：添加/修改 -->
  <ContentWrap v-show="setView==='add'">
    <ConfigurationForm ref="formRef" @success="getList" @setClose="setClose" />
  </ContentWrap>
  <ContentWrap v-show="setView==='view'">
    <!-- 详情弹窗 -->
    <ConfigurationDetail ref="detailRef" @setClose="setClose" />
  </ContentWrap>
</template>
<script lang="ts" setup>
import { allSchemas } from './configuration.data';
import * as mailboxAPI from '@/api/news/mailbox';
import ConfigurationForm from './ConfigurationForm.vue';
import ConfigurationDetail from './ConfigurationDetail.vue';
import { Check, Close } from '@element-plus/icons-vue'

defineOptions({ name: 'Configuration' })
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
// tableObject：表格的属性对象，可获得分页大小、条数等属性
// tableMethods：表格的操作对象，可进行获得分页、删除记录等操作
// 详细可见：https://doc.iocoder.cn/vue3/crud-schema/
const { tableObject, tableMethods } = useTable(
  {
    getListApi: mailboxAPI.getmailServerConfigPage, // 分页接口
    delListApi: mailboxAPI.deletemailServerConfig, // 删除接口
    defaultParams:{} //传递默认数据
  }
)
// 获得表格的各种操作
const { getList, setSearchParams } = tableMethods
const loading = ref(false)
const setView = ref('list');
const setViewFun=(type:string)=>{
  setView.value=type;
}
const setClose=()=>{
  setView.value='list';
}
defineExpose({ setClose }) 
const tableData=[
  {
    name:'机器人1',
    webHookUrl:'https://qyapi.weixin.qq.com/cgibin/webhook/send?key=c73daa86-3321-4731-a233-1abfd16f2839',
    isGroup:true,
    status:true
  }
]
/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number|string) => {
  formRef.value.open(type, id)
  setViewFun('add');
}

/** 详情操作 */
const detailRef = ref()
const openDetail = (id: number|string) => {
  detailRef.value.open(id);
  setViewFun('view');
}

/** 删除按钮操作 */
const handleDelete = async (id) => {
    // 批量删除 获取id数组
    try {
      //确认删除
      await message.delConfirm()
      await mailboxAPI.deletemailServerConfig([id])
      message.success(t('common.delSuccess'))
      // 刷新列表
      await getList()
    } catch {}
  }
// const handleDelete = (id) => {
  
//   tableMethods.delList([id], false)
// }
/** 修改状态 */
const handleStatusChange = async (row: any) => {
  console.log(row,'000000',row.isValid);
  await mailboxAPI.updatemailServerConfigValid({id:row.id,isValid:row.isValid})
}

/** 初始化 **/
onMounted( async () => {
 await getList()
 await openForm('update', tableObject.tableList[0].id)
})
</script>
